package org.btik.light.tool.type;

import java.io.Serializable;
import java.util.Arrays;

/**
 * byte数组不能放入 hashMap作为key 这里简单的封装
 * @author lustre
 * @since 2023/9/21 0:44
 */
public class HashByteArray implements Serializable {
    final byte[] value;

    public HashByteArray(byte[] value) {
        this.value = value;
    }

    public HashByteArray(byte[] src, int start, int length) {
        value = new byte[length];
        System.arraycopy(src, start, value, 0, length);
    }

    public byte at(int index) {
        return value[index];
    }

    public int length() {
        return value.length;
    }

    public boolean isEmpty(){
        return value.length == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HashByteArray)) return false;
        HashByteArray that = (HashByteArray) o;
        return Arrays.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(value);
    }

    @Override
    public String toString() {
        return new String(value);
    }
}
